package com.study.shirojwtredis.shiro.realm;


import com.study.shirojwtredis.shiro.matcher.CustomCredentialsMatcher;
import com.study.shirojwtredis.shiro.matcher.JwtMatcher;
import com.study.shirojwtredis.shiro.token.JwtToken;
import com.study.shirojwtredis.shiro.token.PasswordToken;
import org.apache.shiro.realm.Realm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 *   realm管理器
 * @author tomsun28
 * @date 17:52 2018/3/3
 */
@Configuration
public class RealmManager {

   /* private CustomCredentialsMatcher passwordMatcher;
    private JwtMatcher jwtMatcher;

    @Autowired
    public RealmManager(CustomCredentialsMatcher passwordMatcher,JwtMatcher jwtMatcher) {
        this.passwordMatcher = passwordMatcher;
        this.jwtMatcher = jwtMatcher;
    }*/

   @Bean
   public PasswordRealm passwordRealm(){
       PasswordRealm passwordRealm = new PasswordRealm();
       passwordRealm.setCredentialsMatcher(new CustomCredentialsMatcher());
       passwordRealm.setAuthenticationTokenClass(PasswordToken.class);
       return passwordRealm;
   }

    @Bean
    public JwtRealm jwtRealm(){
        JwtRealm jwtRealm = new JwtRealm();
        jwtRealm.setCredentialsMatcher(new JwtMatcher());
        jwtRealm.setAuthenticationTokenClass(JwtToken.class);
        return jwtRealm;
    }
    public List<Realm> initGetRealm() {
        List<Realm> realmList = new LinkedList<>();
        // ----- password
        realmList.add(passwordRealm());
        // ----- jwt
        realmList.add(jwtRealm());
        return Collections.unmodifiableList(realmList);
    }
}
